﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCB.UI.PopWindows;
using Baosight.Platform.Core.UI;

namespace Baosight.ZNH.ZMCB.UI
{

    /// <summary>
    /// 矿井模块，树形结构 
    /// </summary>
    /// Title:ZMCB01，ServiceZMCB01
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2013-06-21  高强   [创建]
    ///
    public partial class ZMCB01 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        private String myMod = "";//
        public ZMCB01()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCMB0_Loaded);
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            if (initData.GetBlock("result").GetRowCount() == 0)
            {
                this.OpenFristLoginPage(initData, "矿井信息-新增", "insert");
            }
            base.BeforeAction += OnBeforeAction;
        }


        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = new List<EIRow>();
            base.ViewProperty = _view;

        }
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCMB0_Loaded(object sender, RoutedEventArgs e)
        {
            this.mineModelTree.Refresh();
        }

        /// <summary>
        /// 获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> getTreeNodeInfo(EFTreeItem item)
        {

            string value = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
            string display = item.ItemObject.GetType().GetProperty("display").GetValue(item.ItemObject, null) as string;
            string type = item.ItemObject.GetType().GetProperty("type").GetValue(item.ItemObject, null) as string;
            string level = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;
            string isLeaf = (item.IsLeaf == true) ? "1" : "0";
            

            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdParentId", value);
            args.Add("fdMineId", value);
            args.Add("display", display);
            args.Add("level", level);
            args.Add("fdParentType", type);
            args.Add("isLeaf", isLeaf);
            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                //string type = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("type").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("value").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("level").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("level", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;
                    args.Add("level", _value);
                }
            }
            BaseTools.RefreshTree("ZMCB01", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnDoWorkCompleted回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.mineModelTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.mineModelTree.EIDataContext.Update(e.EIInfo);
           // item.IsLoadOnDemandEnabled = true;
            if (!myMod.Equals(""))
            {
                mineModelTree.Refresh();
                myMod = "";
                return;
            }
            if (item.IsLeaf)
            {
                EFTreeItem parent = item.Parent as EFTreeItem;
                string node = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();
                item = BaseTools.FindTreeItem(parent.Items, node, "value") as EFTreeItem;
                this.mineModelTree.SelectedItem = item;
                myMod = "";
                return;
            }
            
                //非叶子节点
                item.Items.Clear();
                item.CollapseItem();
                item.ExpandItem();
            myMod = "";
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mineModelTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0 && treeItem.Index ==0)
            {
                this.mineModelTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }


        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mineModelTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.mineModelTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(item);
            string level = args["level"];
            if (this.value.Value.Equals("0"))
            {
                switch (level)
                {
                    case "0":
                        //水平
                        this.container.LoadView("ZMCB02", args);
                        break;
                    case "1":
                        //采区
                        this.container.LoadView("ZMCB03", args);
                        break;
                    case "2":
                        //采面
                        this.container.LoadView("ZMCB06", args);
                        break;
                    case "3":
                        //巷道
                        this.container.LoadView("ZMCB05", args);
                        break;
                    case "4":
                        //巷道
                        this.container.LoadView("ZMCB07", args);
                        break;
                }
            }
            else if (this.value.Value.Equals("1"))
            {
                 switch (level)
                 {
                 case "0":
                        //采区
                        this.container.LoadView("ZMCB03", args);
                        break;
                    case "2":
                        //采面
                        this.container.LoadView("ZMCB06", args);
                        break;
                    case "3":
                        //巷道
                        this.container.LoadView("ZMCB05", args);
                        break;
                    case "4":
                        //巷道
                        this.container.LoadView("ZMCB07", args);
                        break;
                }
            }
            else
            {
                switch (level)
                {
                    case "0":
                        //采面
                        this.container.LoadView("ZMCB06", args);
                        break;
                    case "3":
                        //巷道
                        this.container.LoadView("ZMCB05", args);
                        break;
                    case "4":
                        //巷道
                        this.container.LoadView("ZMCB07", args);
                        break;
                }
            }
           

        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            if (this.mineModelTree.SelectedItem == null)
            {
                EFWindow.Alert("请选中需要操作的节点");
                base.TempButtonOpen(true);
                return;
            }
            
            EIInfo eiInfo = new EIInfo();
            myMod = "insert";
            this.OpenWindows(eiInfo, "矿井信息-新增", "insert");
        }

        /// <summary>
        /// 点击修改按钮，弹出修改页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EIInfo eiInfo = new EIInfo();
            EFTreeItem treeItem = this.mineModelTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(treeItem);
            string level = args["level"];
            if (treeItem == null)
            {
                base.OpenEFWindowAlert("请选择需要修改的记录！", true);
                return;
            }
            if (!level.Equals("0"))//只能修改矿井节点
            {
                base.OpenEFWindowAlert("不能修改除矿井节点外的其他节点！", true);
                return;
            }

            string fdKeyId = treeItem.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
            eiInfo.Set("service", "ZMCB01");
            eiInfo.Set("fdKeyId", fdKeyId);
            eiInfo.Set("method", "getMineByID");
            myMod = "update";
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, GetMineInforCompleted);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EIInfo eIInfo = new EIInfo();
            eIInfo.Set(EIConstants.ServiceNameKey, "ZMCB01");
            eIInfo.Set(EIConstants.MethodNameKey, "delete");
            EFTreeItem treeItem = this.mineModelTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(treeItem);
            string level = args["level"];
            if (treeItem == null)
            {
                base.OpenEFWindowAlert("请选择需要删除的记录！", true);
                base.TempButtonOpen(true);
                return;
            }
            else
            {
                if (!level.Equals("0"))//不能删除除了根节点的节点
                {
                    base.OpenEFWindowAlert("不能删除非矿井节点！", true);
                    base.TempButtonOpen(true);
                    return;
                }
                DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除该矿井吗？");
                param.Closed = OnDeleteConfirmClosed;
                EFWindow.Confirm(param);
            }
        }

        /// <summary>
        /// 删除确认，点击删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDeleteConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                EIInfo eIInfo = new EIInfo();
                eIInfo.Set(EIConstants.ServiceNameKey, "ZMCB01");
                eIInfo.Set(EIConstants.MethodNameKey, "delete");
                EFTreeItem treeItem = this.mineModelTree.SelectedItems[0] as EFTreeItem;
                string fdKeyId = treeItem.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                eIInfo.Set("fdKeyId", fdKeyId);
                myMod = "delete";
                base.Submit(eIInfo, ThisDeleteOperationCompleted);
            }
        }

        /// <summary>
        /// 删除回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ThisDeleteOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                String msg = "删除失败！";
                if (e.EIInfo.GetMsg() != null || e.EIInfo.GetMsg().Equals(""))
                {
                    msg = e.EIInfo.Msg;
                }
                base.OpenEFWindowAlert(msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                this.OnEIDataPublished(e.EIInfo);

                base.TempButtonOpen(true);
            }
        }

        /// <summary>
        /// 根据ID获取对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetMineInforCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() != -1)
            {
                this.OpenWindows(e.EIInfo, "矿井信息-修改", "update");
            }
            else
            {
                base.OpenEFWindowAlert(e.EIInfo.GetMsg(), true);
                base.TempButtonOpen(true);
            }
        }


        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void OpenWindows(EIInfo eIInfo, string title, string dialogType)
        {
            AddMine mine = new AddMine(dialogType);//构造一个新的修改页面
            mine.EIDataContext = new Platform.Core.UI.EIDataContext();
            mine.EIDataContext.Update(eIInfo);//数据渲染
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = mine;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 首次进入矿井建模批量新增矿井信息
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void OpenFristLoginPage(EIInfo eIInfo, string title, string dialogType)
        {

            FirstLogin firstLogin = new FirstLogin();//构造一个新的修改页面
            firstLogin.EIDataContext = new Platform.Core.UI.EIDataContext();
            firstLogin.value.Value = this.value.Value;
            firstLogin.EIDataContext.Update(eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
            _popup.TopOffset = -150;
            _popup.LeftOffset = -80;
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = firstLogin;//让新增页面在弹出窗口显示

            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);

            _popup.ShowDialog();//模态显示弹出窗口
        }

        ///// <summary>
        /////关闭弹出窗口
        ///// </summary>
        ///// <param name="eIInfo"></param>
        ///// <param name="title"></param>
        //private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        //{

        //    //按钮生效
        //   base.TempButtonOpen(true);
        //   this.OnEIDataPublished(new EIInfo());
        //}

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            BaseTools.RefreshTree("ZMCB01", "query", args, OnDoWorkCompleted);
            myMod = "FristLogin";
           
            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
